package com.ddx.task;

import com.ddx.common.enums.OrderStatus;
import com.ddx.common.enums.ResultType;
import com.ddx.dao.ShoppingOrderMapper;
import com.ddx.dao.ShoppingPayFlowMapper;
import com.ddx.dao.UserMapper;
import com.ddx.pay.JsonUtil;
import com.ddx.pay.MchOrderInfoResult;
import com.ddx.pay.WeixinPay;
import com.ddx.pojo.ShoppingOrder;
import com.ddx.pojo.ShoppingPayFlow;
import com.ddx.pojo.ShoppingPayFlowWithBLOBs;
import com.ddx.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2019/2/25
 */
@Component
public class PayFlowTask {
    private static Logger log = LoggerFactory.getLogger(PayFlowTask.class);
    @Autowired
    ShoppingPayFlowMapper payFlowMapper;
    @Autowired
    WeixinPay weixinPay;
    @Autowired
    ShoppingOrderMapper shoppingOrderMapper;
    @Autowired
    UserMapper userMapper;

    @Scheduled(fixedDelay = 600000)
    public void afterPropertiesSet() throws Exception {
        ShoppingPayFlow record=new ShoppingPayFlow();
        record.setDealStatus(ResultType.PROCESSING.getCode());
        List<ShoppingPayFlowWithBLOBs> payFlowWithBLOBsList= payFlowMapper.selectByRecord(record);
        if(payFlowWithBLOBsList==null){
            return;
        }
        log.info("支付中订单条数{}",payFlowWithBLOBsList.size());
        for(ShoppingPayFlowWithBLOBs payFlowWithBLOBs:payFlowWithBLOBsList){
            MchOrderInfoResult mchOrderInfoResult=weixinPay.payOrderquery(payFlowWithBLOBs.getOrderId());
            log.info("查询支付中订单返回结果orderId={},返回结果{}",payFlowWithBLOBs.getOrderId(),JsonUtil.toJSONString(mchOrderInfoResult));
            if("SUCCESS".equals(mchOrderInfoResult.getTrade_state())){
                if("1".equals(payFlowWithBLOBs.getFeeType())){
                    ShoppingPayFlowWithBLOBs update=new ShoppingPayFlowWithBLOBs();
                    update.setReturnSendRecord(JsonUtil.toJSONString(mchOrderInfoResult));
                    update.setOrderId(payFlowWithBLOBs.getOrderId());
                    update.setDealStatus(ResultType.SUCCESS.getCode());
                    update.setThirdPartyNo(mchOrderInfoResult.getTransaction_id());
                    if(StringUtils.isNotBlank(mchOrderInfoResult.getTrade_state_desc())) {
                        update.setMessage(new String(mchOrderInfoResult.getTrade_state_desc().getBytes("iso8859-1"), "utf-8"));
                    }
                    update.setTimeEnd(mchOrderInfoResult.getTime_end());
                    payFlowMapper.updateByOrderId(update);
                    User updateUser=new User();
                    updateUser.setId(Integer.parseInt(payFlowWithBLOBs.getUserId()));
                    updateUser.setIsBond(1);
                    userMapper.updateByPrimaryKeySelective(updateUser);
                }else if("2".equals(payFlowWithBLOBs.getFeeType())){
                    ShoppingPayFlowWithBLOBs updatePay=new ShoppingPayFlowWithBLOBs();
                    updatePay.setReturnSendRecord(JsonUtil.toJSONString(mchOrderInfoResult));
                    updatePay.setOrderId(payFlowWithBLOBs.getOrderId());
                    updatePay.setDealStatus(ResultType.SUCCESS.getCode());
                    updatePay.setTimeEnd(mchOrderInfoResult.getTime_end());
                    updatePay.setThirdPartyNo(mchOrderInfoResult.getTransaction_id());
                    payFlowMapper.updateByOrderId(record);
                    ShoppingOrder update=new ShoppingOrder();
                    update.setOrderId(payFlowWithBLOBs.getOrderId());
                    update.setStatus(OrderStatus.DAIFAHUO.getValue());
                    shoppingOrderMapper.updateByOrderId(update);
                }
            }else if("NOTPAY".equals(mchOrderInfoResult.getTrade_state())||"FAIL".equals(mchOrderInfoResult.getTrade_state())){
                ShoppingPayFlowWithBLOBs updatePay=new ShoppingPayFlowWithBLOBs();
                updatePay.setReturnSendRecord(JsonUtil.toJSONString(mchOrderInfoResult));
                updatePay.setOrderId(payFlowWithBLOBs.getOrderId());
                updatePay.setDealStatus(ResultType.FAILED.getCode());
                if(StringUtils.isNotBlank(mchOrderInfoResult.getTrade_state_desc())) {
                    record.setMessage(new String(mchOrderInfoResult.getTrade_state_desc().getBytes("iso8859-1"), "utf-8"));
                }
                updatePay.setTimeEnd(mchOrderInfoResult.getTime_end());
                payFlowMapper.updateByOrderId(updatePay);
            }
        }
    }

}
